﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.Common.Script;

public partial class Theme_index_BBS :PowerCreator.LMS.Web.Base.WebBasePage
{
    PowerCreator.LMS.Web.BBS.ManagerBBS opBBS = new PowerCreator.LMS.Web.BBS.ManagerBBS();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected string itype = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = config.GetLanagerValueByKey("theme_menu_Forum", PowerCreator.LMS.Language.LanagetItemType.Page);
        if (!IsPostBack)
        {
            ViewState["DomainID"] = "0";
            string domainID = Request.QueryString["DomainID"].ToString();
            ViewState["DomainID"] = domainID;

            ViewState["Type"] = "0";
            itype = Request.QueryString["Type"].ToString();
            ViewState["Type"] = itype;

           
            ViewState["SortOrder"] = "ThemeUserName";
            ViewState["OrderDire"] = "ASC";

            BinderTree();

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
   
    protected override void InitPage()
    {
        Button1.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Add.Text = GetLanageValueByKey("web_bbs_titlelist_btn_add", PowerCreator.LMS.Language.LanagetItemType.Page);



        base.InitPage();
    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        if (ViewState["Type"] != null)
        {
            sbSearch.Append(string.Format(" ThemeType = {0} and ", ViewState["Type"]));
        }
        if (ViewState["DomainID"].ToString() != "0")
        {
            sbSearch.Append(string.Format(" ThemeDomainID = {0} and ", ViewState["DomainID"]));
        }
        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
        {
            sbSearch.Append(string.Format(" (ThemeTitle like '%{0}%' or ThemeContent like '%{0}%' or ThemeUserName like '%{0}%' or ThemeLastRevertUserName like '%{0}%') and", txt_KeyWord.Text));
        }
        sbSearch.Append(" ThemeIsVisible = 1 ");
        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = PowerCreator.LMS.Web.Config.WebConfig.PageSize;
        AspNetPager1.RecordCount = opBBS.CountTheme(search);

        PreAspNetPager(AspNetPager1);
    }
    private void BinderRPT(string search,int pageIndex)
    {
        DataTable dt = new DataTable();
        string orderBy = " ThemeCreatetime desc ";
        dt = opBBS.QueryTheme(search, orderBy, PowerCreator.LMS.Web.Config.WebConfig.PageSize, pageIndex);
        dt.Columns.Add("bg");
        int iResult = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            int k = Math.DivRem(i, 2, out iResult);
            if (iResult == 0)
                dt.Rows[i]["bg"] = "#FFFFFF";
            if (iResult == 1)
                dt.Rows[i]["bg"] = "#EEEEEE";
        }
        rpt_BBS.DataSource = dt;
        rpt_BBS.DataBind();
    }

    //绑定树控件
    private void BinderTree()
    {
        string search = "";
    }

 

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_seach_Click(object sender, EventArgs e)
    {
        //ViewState["DomainID"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Common.Script.Script.RedirectURL("AddTheme.aspx?Type=" + ViewState["Type"].ToString() + "&DomainID=" + ViewState["DomainID"].ToString());
    }
    //protected void tv_Domain_SelectedNodeChanged(object sender, EventArgs e)
    //{
    //    ViewState["DomainID"] = tv_Domain.SelectedNode.Value;

    //    string search = GetSearch();
    //    BinderPager(search);
    //    BinderRPT(search, 1);
    //}
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        //string sPage = e.SortExpression;
        //if (ViewState["SortOrder"].ToString() == sPage)
        //{
        if (ViewState["OrderDire"].ToString() == "Desc")
            ViewState["OrderDire"] = "ASC";
        else
            ViewState["OrderDire"] = "Desc";
        //}
        //else
        //{
        ViewState["SortOrder"] = e.SortExpression;
        //}

        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
}
